/**
 * 生成物流单号操作：新增、移除、销毁
 */
import { storageLocal } from "@pureadmin/utils";
import { orderDestroyBillNo } from "@/api/orderManag/purchaseOrder";
const billNoKey = "erpPlatBillNo";

// 本地保存快递单号
export const saveBillNo = billNoData => {
  storageLocal().setItem(billNoKey, billNoData);
};

// 本地清空单号
export const clearBillNo = () => {
  storageLocal().removeItem(billNoKey);
};

// 销毁本地单号
export const destroyBillNo = () => {
  return new Promise(resolve => {
    let billNoData: any = storageLocal().getItem(billNoKey);
    console.log("本地未销毁的运单号数据:", billNoData);
    if (billNoData) {
      let params = {
        nums: billNoData.billNoList.map(v => v.kuaidinum) || [],
        suborderId: billNoData.subOrderId
      };
      orderDestroyBillNo(params).then(res => {
        if (res.code == 200) {
          clearBillNo();
          resolve(true);
        } else {
          resolve(false);
        }
      });
    } else {
      resolve(true);
    }
  });
};
